package com.dream.service2.utils;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ToolKits {
    @SuppressWarnings("rawtypes")
    public static boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        }

        if (obj instanceof Collection) {
            return ((Collection) obj).size() == 0;

        } else if (obj instanceof Map) {
            return ((Map) obj).isEmpty();

        } else if (obj.getClass().isArray()) {
            Class cmpType = obj.getClass().getComponentType();
            if (cmpType == long.class) {
                return ((long[]) obj).length == 0;
            } else if (cmpType == int.class) {
                return ((int[]) obj).length == 0;
            } else if (cmpType == short.class) {
                return ((short[]) obj).length == 0;
            } else if (cmpType == double.class) {
                return ((double[]) obj).length == 0;
            } else if (cmpType == float.class) {
                return ((float[]) obj).length == 0;
            } else {
                return ((Object[]) obj).length == 0;
            }
        } else {
            return obj.toString().trim().length() == 0;
        }
    }

    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    public static boolean isNotEmpty(byte[] b) {
        return b != null;
    }

    /**
     * 删除null对象
     */
    @SuppressWarnings({"rawtypes"})
    public static void removeNullObject(Object obj) {
        if (obj != null) {
            if (obj instanceof Map) {
                Map map = (Map) obj;
                Iterator iterator = map.entrySet().iterator();
                while (iterator.hasNext()) {
                    Map.Entry entry = (Map.Entry) iterator.next();
                    Object value = entry.getValue();
                    if (value == null || "".equals(value)) {
                        iterator.remove();
                    } else {
                        removeNullObject(value);
                    }
                }
            } else if (obj instanceof List) {
                List list = (List) obj;
                Iterator iterator = list.iterator();
                while (iterator.hasNext()) {
                    Object value = iterator.next();
                    if (value == null) {
                        iterator.remove();
                    } else {
                        removeNullObject(value);
                    }
                }
            }
        }
    }

    public static String convertToString(Object obj) {
        if (ToolKits.isNotEmpty(obj)) {
            return String.valueOf(obj);
        }
        return "";
    }

    public static Integer convertToInteger(Object obj) {
        if (ToolKits.isNotEmpty(obj)) {
            return Integer.valueOf(convertToString(obj));
        }
        return null;
    }

    public static Double convertToDouble(Object obj) {
        if (ToolKits.isNotEmpty(obj)) {
            return Double.valueOf(convertToString(obj));
        }
        return null;
    }

    public static Long convertToLong(Object obj) {
        if (ToolKits.isNotEmpty(obj)) {
            return Long.valueOf(convertToString(obj));
        }
        return null;
    }

    public static boolean convertToBoolean(String str) {
        if (ToolKits.isNotEmpty(str)) {
            return Boolean.parseBoolean(str);
        }
        return false;
    }

    public static BigDecimal convertBogDecimal(Object obj) {
        if (ToolKits.isNotEmpty(obj)) {
            return new BigDecimal(convertToString(obj));
        }
        return null;
    }

    public static short convertToShort(Object obj) {
        if (ToolKits.isNotEmpty(obj)) {
            return Short.parseShort(convertToString(obj));
        }
        return 0;
    }

    public static Boolean compareBigDecimal(BigDecimal amt, BigDecimal tmp) {
        return amt.compareTo(tmp) > 0;
    }

    /**
     * 去掉字符串首尾的空格
     */
    public static String replaceTab(String text) {
        String replaceStr = text;
        String regEx = "[' ']+,['\t']+";
        for (int i = 0; i < regEx.split(",").length; i++) {
            Pattern p = Pattern.compile(regEx.split(",")[i]);
            Matcher m = p.matcher(replaceStr);
            replaceStr = m.replaceAll(" ").trim();
        }
        return replaceStr;
    }

    /**
     * 去掉字符串text
     */
    public static String replace(String s, String text) {
        if (isEmpty(s)) {
            return "";
        }
        return s.replace(text, "");
    }

    public static String replace(String s, String o, String n) {
        if (!s.contains(o)) {
            return s;
        }
        return s.replace(o, n);
    }

    /**
     * 将首字母编程大写
     */
    public static String stringToUp(String str) {
        String reg = "^[0-9]*$";
        if (str.matches(reg)) {
            return str;
        }
        String regex = "^[A-Z].*?";
        if (str.matches(regex)) {
            return str;
        }
        char[] cs = str.toCharArray();
        cs[0] -= 32;
        return String.valueOf(cs);
    }

    public static String subNum(String str) {
        if (!hasDigit(str)) {
            return str;
        }
        return splitNotNumber(str);
    }

    // 截取非数字
    public static String splitNotNumber(String content) {
        Pattern pattern = Pattern.compile("\\D+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }

    // 判断一个字符串是否含有数字
    public static boolean hasDigit(String content) {
        Pattern p = Pattern.compile(".*\\d+.*");
        Matcher m = p.matcher(content);
        return m.matches();

    }

    /**
     * 将首字母编程小写
     */
    public static String stringToLower(String str) {
        String reg = "^[0-9]*$";
        if (str.matches(reg)) {
            return str;
        }
        String regex = "^[a-z].*?";
        if (str.matches(regex)) {
            return str;
        }
        char[] cs = str.toCharArray();
        cs[0] += 32;
        return String.valueOf(cs);
    }

    public static Integer areaOptions(Integer[] selectedOptions) {
        if (ToolKits.isEmpty(selectedOptions)) {
            return null;
        }
        return selectedOptions[selectedOptions.length - 1];
    }

    public static Integer[] selectedOptions(Map<String, Object> areaMap) {
        if (ToolKits.isEmpty(areaMap)) {
            return null;
        }
        Integer pid = convertToInteger(areaMap.get("cid"));
        Integer cid = convertToInteger(areaMap.get("bid"));
        Integer aid = convertToInteger(areaMap.get("aid"));
        return new Integer[]{pid, cid, aid};
    }
}
